package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Project;
import com.jr.service.IprojectService;
import com.jr.service.impl.ProjectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/pro/*")
public class ProjectController extends HttpServlet {

    private IprojectService ips=new ProjectServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String substring = requestURI.substring(index);
        if (substring.length()==0){
            substring="showdt";
        }

        Class empControllerClass = this.getClass();
        Method method=null;

        try {
            method=empControllerClass.getMethod(substring,HttpServletRequest.class,HttpServletResponse.class);

            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");


            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void getAll (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        ArrayList<Project> projects=ips.queryAll();
        response.getWriter().println(new Gson().toJson(projects));
    }


    }
